//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.zaicheng.cloud.remotecall.template;


import com.zaicheng.cloud.remotecall.Util;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public final class QueryTemplate {
    private static final String UNDEF = "undef";
    private List<Template> values = new CopyOnWriteArrayList();
    private final Template name;
//    private final CollectionFormat collectionFormat;
    private boolean pure = false;

    public static QueryTemplate create(String name, Iterable<String> values, Charset charset) {
        return create(name, values, charset,  true);
    }

//    public static QueryTemplate create(String name, Iterable<String> values, Charset charset) {
//        return create(name, values, charset, true);
//    }

    public static QueryTemplate create(String name, Iterable<String> values, Charset charset, boolean decodeSlash) {
        if (Util.isBlank(name)) {
            throw new IllegalArgumentException("name is required.");
        } else if (values == null) {
            throw new IllegalArgumentException("values are required");
        } else {
            Collection<String> remaining = (Collection)StreamSupport.stream(values.spliterator(), false).filter(Util::isNotBlank).collect(Collectors.toList());
            return new QueryTemplate(name, remaining, charset, decodeSlash);
        }
    }

    public static QueryTemplate append(QueryTemplate queryTemplate, Iterable<String> values, boolean decodeSlash) {
        List<String> queryValues = new ArrayList(queryTemplate.getValues());
        queryValues.addAll((Collection)StreamSupport.stream(values.spliterator(), false).filter(Util::isNotBlank).collect(Collectors.toList()));
        return create(queryTemplate.getName(), queryValues, StandardCharsets.UTF_8, decodeSlash);
    }

    private QueryTemplate(String name, Iterable<String> values, Charset charset,boolean decodeSlash) {
        this.name = new Template(name, Template.ExpansionOptions.ALLOW_UNRESOLVED, Template.EncodingOptions.REQUIRED, !decodeSlash, charset);
        //this.collectionFormat = collectionFormat;
        Iterator var6 = values.iterator();

        while(var6.hasNext()) {
            String value = (String)var6.next();
            if (!value.isEmpty()) {
                this.values.add(new Template(value, Template.ExpansionOptions.REQUIRED, Template.EncodingOptions.REQUIRED, !decodeSlash, charset));
            }
        }

        if (this.values.isEmpty()) {
            this.pure = true;
        }

    }

    public List<String> getValues() {
        return Collections.unmodifiableList((List)this.values.stream().map(Template::toString).collect(Collectors.toList()));
    }

    public List<String> getVariables() {
        List<String> variables = new ArrayList(this.name.getVariables());
        Iterator var2 = this.values.iterator();

        while(var2.hasNext()) {
            Template template = (Template)var2.next();
            variables.addAll(template.getVariables());
        }

        return Collections.unmodifiableList(variables);
    }

    public String getName() {
        return this.name.toString();
    }

    @Override
    public String toString() {
        return this.queryString(this.name.toString(), this.getValues());
    }

    public String expand(Map<String, ?> variables) {
        String name = this.name.expand(variables);
        if (this.pure) {
            return name;
        } else {
            List<String> expanded = new ArrayList();
            Iterator var4 = this.values.iterator();

            while(var4.hasNext()) {
                Template template = (Template)var4.next();
                String result = template.expand(variables);
                if (result != null) {
                    if (result.contains(",")) {
                        expanded.addAll(Arrays.asList(result.split(",")));
                    } else {
                        expanded.add(result);
                    }
                }
            }

            return this.queryString(name, Collections.unmodifiableList(expanded));
        }
    }

    private String queryString(String name, List<String> values) {
        if (this.pure) {
            return name;
        } else {
            //return !values.isEmpty() ? this.collectionFormat.join(name, values, StandardCharsets.UTF_8).toString() : null;
            return null;
        }
    }
}
